package com.magenta.maxoptra.geocoding.ptv.util;

import com.ptvag.jabba.service.baseservices.CallerContext;
import com.ptvag.jabba.service.baseservices.CallerContextProperty;
import com.ptvag.xserver.common.*;

/**
 * this utility class contains static methods for creating common datastructures,
 * such as geometry objects and caller context properties.
 * 
 * @author RN
 */
public final class CommonUtils {
	
	private CommonUtils() {
		// this is a utility class!
	}

	//---------------- Geometry Utilities ------------------------------

	public static PlainPoint makePlainPoint(double x, double y) {
		return new PlainPoint(x, y);
	}

	public static Point makePoint(double x, double y) {
		return new Point(makePlainPoint(x, y));
	}

	public static PlainLineString makePlainLineString(double[] coordinates) {
		PlainPoint[] pts = new PlainPoint[coordinates.length / 2];
		for (int i = 0; i < coordinates.length / 2; i++) {
			pts[i] = new PlainPoint(coordinates[2*i], coordinates[2*i+1]);
		}
		return new PlainLineString(pts);
	}
	
	public static LineString makeLineString(double[] coordinates) {
		return new LineString(makePlainLineString(coordinates));
	}
	
	public static PlainLinearRing makePlainLinearRing(double[] coordinates) {
		PlainPoint[] pts = new PlainPoint[coordinates.length / 2];
		for (int i = 0; i < coordinates.length / 2; i++) {
			pts[i] = new PlainPoint(coordinates[2*i], coordinates[2*i+1]);
		}
		return new PlainLinearRing(pts);
	}
	
	public static LinearRing makeLinearRing(double[] coordinates) {
		return new LinearRing(makePlainLinearRing(coordinates));
	}
	
	public static PlainPolygon makePlainPolygon(double[][] coordinates) {
		PlainLinearRing[] rings = new PlainLinearRing[coordinates.length];
		for (int i = 0; i < coordinates.length; i++) {
			rings[i] = makePlainLinearRing(coordinates[i]);
		}
		return new PlainPolygon(rings);
	}
	
	public static Polygon makePolygon(double[][] coordinates) {
		return new Polygon(makePlainPolygon(coordinates));
	}
	
	//---------------- Caller Context Utilities ------------------------------
	
	public static CallerContext makeDefaultContext() {
		return makeContext("default", "", "", "");
	}
	
	public static CallerContext makeContext(String profile, String log1, String log2, String log3) {
		return makeContext(profile, CoordFormat.PTV_MERCATOR, new GeometryEncoding[] { GeometryEncoding.PLAIN }, log1, log2, log3);
	}
	
	public static CallerContext makeContext(String profile, CoordFormat coordFormat, String responseGeometries, String log1, String log2, String log3) {
		String[] encStrs = responseGeometries.split(",");
		GeometryEncoding[] encs = new GeometryEncoding[encStrs.length];
		for (int i = 0; i < encs.length; i++) {
			encs[i] = GeometryEncoding.fromString(encStrs[i]);
		}
		return makeContext(profile, coordFormat, encs, log1, log2, log3);
	}

	public static CallerContext makeContext(String profile, CoordFormat coordFormat, GeometryEncoding[] responseGeometries, String log1, String log2, String log3) {
		CallerContext res = new CallerContext();
		res.setLog1(log1);
		res.setLog2(log2);
		res.setLog3(log3);
		CallerContextProperty[] props = new CallerContextProperty[3];
		props[0] = new CallerContextProperty("Profile", profile); 
		props[1] = new CallerContextProperty("CoordFormat", coordFormat.getValue()); 
		String respGeom = "";
		for (int i = 0; i < responseGeometries.length; i++) {
			respGeom += ',' + responseGeometries[i].getValue();
		}
		if (respGeom.length() == 0) {
			respGeom = GeometryEncoding.PLAIN.getValue();
		} else {
			respGeom = respGeom.substring(1);
		}
		props[2] = new CallerContextProperty("ResponseGeometry", respGeom); 
		res.setProperties(props);
		return res;
	}
	
}
